﻿using NB.MGJ.Common.MVC;
using NB.MGJ.Common.OSS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NB.MGJ.Web.Controllers
{
    public class UploadController : BaseController
    {

        public ActionResult Upload()
        {
            HttpFileCollectionBase files = HttpContext.Request.Files;
            string result = string.Empty;
            string filePath = string.Empty;
            string fileName = string.Empty;

            if (files.Count > 0)
            {
                HttpPostedFileBase postedFile = files[0];
        
                //fileName = System.Guid.NewGuid().ToString() + System.IO.Path.GetFileName(postedFile.FileName);
                fileName = System.Guid.NewGuid().ToString();

                if (!string.IsNullOrEmpty(fileName))
                {
                    var stream = postedFile.InputStream;
                    var uploadResult= OssManager.Instance.PutObject(fileName, stream);
                    if (uploadResult)
                    {
                        filePath = OssManager.Instance.GetUrl(fileName);
                        result = "上传成功。" + filePath;
                    }
                    else
                    {
                        result = "上传失败。" + "uploaderror";
                    }
                }

                else
                {
                    result = "请选择文件。";
                }
            }
            else
            {
                result = "请选择文件。";
            }
            return Content(result);
        }




	}
}